var feedgraph = new Object();

feedgraph.getobj = function (uri, callback) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("GET", "http://api.feedgraph.com/" + uri, true);

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
				callback(JSON.parse(req.responseText));
			} else {

				if (console) console.log(req);
			}
		}
	};

	req.send();

	return req;
}

/* calls callback once for each item in the feed */
feedgraph.getfeed = function (uri, callback) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("GET", "http://api.feedgraph.com/" + uri, true);

	var arrCallback = function (arr) {

		for (var i = 0; i < arr.length; i++) {

			callback(arr[i].Object, arr[i]);
		}
	};

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
				arrCallback(JSON.parse(req.responseText));
			} else {

				if (console) console.log(req);
			}
		}
	};

	req.send();

	return req;
}

feedgraph.putobj = function (uri, obj) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("PUT", "http://api.feedgraph.com/" + uri, true);

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
			} else {
				alert("failed to send object to server");
			}
		}
	};

	req.send(JSON.stringify(obj));

	return req;
}

feedgraph.postobj  = function (uri, obj) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("POST", "http://api.feedgraph.com/" + uri, true);

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
			} else {
				alert("failed to send object to server");
			}
		}
	};

	req.send(JSON.stringify(obj));

	return req;
}

feedgraph.get = function (uri, callback) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("GET", "http://api.feedgraph.com/" + uri, true);

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
				callback(req.responseText);
			} else {
			}
		}
	};

	req.send();

	return req;
}

feedgraph.put = function (uri, text) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("PUT", "http://api.feedgraph.com/" + uri, true);

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
			} else {
				alert("failed to send object to server");
			}
		}
	};

	req.send(text);

	return req;
}

feedgraph.post = function (uri, text) {

	if (uri.charAt(0) == "/") uri = uri.substr(1);
	
	var req = new XMLHttpRequest();
	req.open("POST", "http://api.feedgraph.com/" + uri, true);

	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
			} else {
				alert("failed to send object to server");
			}
		}
	};

	req.send(text);

	return req;
}

/* ignoreOld=true specifies to only trigger callback for new entries */
feedgraph.watch = function (uri, callback, ignoreOld, since) {

	var req = null;

	if (ignoreOld == true) since = new Date().toISOString();

	if (since == null) since = new Date(0).toISOString();

	var poll = function () {
		var url = uri + "?block=true&since=" + escape(since);
		req = feedgraph.getobj(url, function (arr) {


			for (var i = 0; i < arr.length; i++) {

				since = arr[i].CreatedAt;
				
				callback(arr[i].Object, arr[i]);
			}

			poll();	
		});
	}

	poll();

	return { 
		"cancel" : function() {
			req.abort();
		},

		"restart" : function () {
			req.abort();
			since = new Date(0).toISOString();
			poll();
		}, 

		"resume" : poll
	};
}
